#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_NODES 1000 // 最大节点数量

// 哈夫曼树的节点结构
typedef struct HuffmanNode
{
    int freq;
    struct HuffmanNode *left;  // 左子节点
    struct HuffmanNode *right; // 右子节点
} HuffmanNode;

// 最小堆的结构
typedef struct MinHeap
{
    HuffmanNode *data[MAX_NODES];
    int size;
} MinHeap;

// 创建一个哈夫曼节点
HuffmanNode *create_node(int freq)
{
    HuffmanNode *node = (HuffmanNode *)malloc(sizeof(HuffmanNode));
    node->freq = freq;
    node->left = node->right = NULL;
    return node;
}

// 初始化最小堆
MinHeap *create_min_heap()
{
    MinHeap *heap = (MinHeap *)malloc(sizeof(MinHeap));
    heap->size = 0;
    return heap;
}

// 交换两个堆节点
void swap(HuffmanNode **a, HuffmanNode **b)
{
    HuffmanNode *temp = *a;
    *a = *b;
    *b = temp;
}

// 向下调整堆
void heapify_down(MinHeap *heap, int idx)
{
    int smallest = idx;
    int left = 2 * idx + 1;
    int right = 2 * idx + 2;

    if (left < heap->size && heap->data[left]->freq < heap->data[smallest]->freq)
    {
        smallest = left;
    }
    if (right < heap->size && heap->data[right]->freq < heap->data[smallest]->freq)
    {
        smallest = right;
    }

    if (smallest != idx)
    {
        swap(&heap->data[idx], &heap->data[smallest]);
        heapify_down(heap, smallest);
    }
}

// 向上调整堆
void heapify_up(MinHeap *heap, int idx)
{
    int parent = (idx - 1) / 2;
    if (idx > 0 && heap->data[idx]->freq < heap->data[parent]->freq)
    {
        swap(&heap->data[idx], &heap->data[parent]);
        heapify_up(heap, parent);
    }
}

// 向最小堆中插入一个节点
void insert_heap(MinHeap *heap, HuffmanNode *node)
{
    heap->data[heap->size] = node;
    heap->size++;
    heapify_up(heap, heap->size - 1);
}

// 从最小堆中取出最小节点
HuffmanNode *extract_min(MinHeap *heap)
{
    if (heap->size == 0)
        return NULL;
    HuffmanNode *min_node = heap->data[0];
    heap->data[0] = heap->data[heap->size - 1];
    heap->size--;
    heapify_down(heap, 0);
    return min_node;
}

// 构造哈夫曼树
HuffmanNode *build_huffman_tree(MinHeap *heap)
{
    while (heap->size > 1)
    {
        HuffmanNode *left = extract_min(heap);
        HuffmanNode *right = extract_min(heap);
        HuffmanNode *new_node = create_node(left->freq + right->freq);
        new_node->left = left;
        new_node->right = right;
        insert_heap(heap, new_node);
    }
    return extract_min(heap);
}

// 递归计算哈夫曼编码长度
void calculate_code_length(HuffmanNode *node, int depth, int *total_weight, int *total_length)
{
    if (node == NULL)
        return;

    // 叶子节点
    if (node->left == NULL && node->right == NULL)
    {
        *total_weight += node->freq;
        *total_length += node->freq * depth;
    }

    calculate_code_length(node->left, depth + 1, total_weight, total_length);
    calculate_code_length(node->right, depth + 1, total_weight, total_length);
}

// 释放哈夫曼树的内存
void free_huffman_tree(HuffmanNode *node)
{
    if (node == NULL)
        return;
    free_huffman_tree(node->left);
    free_huffman_tree(node->right);
    free(node);
}

int main()
{
    int N;
    scanf("%d", &N);

    MinHeap *heap = create_min_heap();

    // 读取输入的单词及其频度
    for (int i = 0; i < N; i++)
    {
        char symbol[10];
        int freq;
        scanf("%s %d", symbol, &freq);
        HuffmanNode *node = create_node(freq);
        insert_heap(heap, node);
    }

    // 构造哈夫曼树
    HuffmanNode *root = build_huffman_tree(heap);

    // 计算平均码长
    int total_weight = 0, total_length = 0;
    calculate_code_length(root, 0, &total_weight, &total_length);
    double average_length = (double)total_length / total_weight;
    printf("%.5f\n", average_length);

    // 释放内存
    free_huffman_tree(root);
    free(heap);
    return 0;
}
